﻿using System;
using System.Collections.ObjectModel;
using System.Text;
using MUD.Lib.Utils;

namespace MUD.Lib.Objects
{
    internal class Container : VerbProcessor
    {
        /// <summary>
        /// Optional capacity.
        /// </summary>
        public int? Capacity = null;

        protected Collection<MudObject> Items = new Collection<MudObject>();

        public Container()
            :base()
        {

        }

        /// <summary>
        /// Is this container empty?
        /// </summary>
        public bool Empty
        {
            get
            {
                return Items.Count == 0;
            }
        }

        /// <summary>
        /// Is this container full? (Must have a capacity set first)
        /// </summary>
        public bool Full
        {
            get
            {
                if (Capacity != null)
                {
                    return Capacity <= Items.Count;
                }
                return false;
            }
                
        }

        /// <summary>
        /// Add an item.
        /// </summary>
        /// <param name="item">The generic MudObject to add to this container.</param>
        public virtual void Add(MudObject item)
        {
            if (!Items.Contains(item))
            {
                if (Capacity != null)
                {
                    if (Capacity < Items.Count)
                        Items.Add(item);
                }
                else
                {
                    Items.Add(item);
                }
            }
        }

        /// <summary>
        /// Remove a specific item.
        /// </summary>
        /// <param name="item">The MudObject to remove from the container.</param>
        public virtual void Remove(MudObject item)
        {
            if (Items.Contains(item))
                Items.Remove(item);
        }

        /// <summary>
        /// An array of references to the MudObjects in this container.
        /// </summary>
        public MudObject[] ItemList
        {
            get
            {
                MudObject[] temp = new MudObject[Items.Count];
                // NO COPYING! Grab references to the actual objects!
                for(int i = 0; i < Items.Count; i++)
                {
                    temp[i] = Items[i];
                }
                return temp;
            }
        }

        /// <summary>
        /// Grab a MudObject based on ID.
        /// </summary>
        /// <param name="Id">String ID</param>
        /// <returns>MudObject if found, null if not.</returns>
        public MudObject this[string Id]
        {
            get
            {
                foreach (MudObject obj in Items)
                {
                    foreach (string id in obj.ID)
                    {
                        if (id == Id)
                            return obj;
                    }
                }
                return null;
            }
        }

        /// <summary>
        /// Grab a MudObject based on the Guid.
        /// </summary>
        /// <param name="guid">Guid to search for.</param>
        /// <returns>MudOBject if found, null if not.</returns>
        public MudObject this[Guid guid]
        {
            get
            {
                foreach (MudObject obj in Items)
                {
                    if (guid == obj.guid)
                        return obj;
                }
                return null;
            }
        }
    }
}
